﻿Imports Krs.Ats.IBNet
Imports System.Collections.Specialized


Class MasterWindow
    Protected Overrides Sub OnInitialized(ByVal e As System.EventArgs)
        MyBase.OnInitialized(e)
    End Sub

    Public totalMouseWheel As Integer

    Private _ib As New IBDataStream
    Public ReadOnly Property IB As IBDataStream
        Get
            Return _ib
        End Get
    End Property

    Public Sub CloseApplication()
        SaveDesktopLayouts()
        For Each desktop In Desktops
            desktop.Close()
        Next
    End Sub
    Public Sub SaveDesktopLayouts()
        Dim dir As String = ApplicationSettingsFileLocation
        IO.File.WriteAllText(dir, "")
        Dim ids As New List(Of String)
        Dim desktops As List(Of DesktopWindow) = Me.Desktops
        For Each desktop In desktops
            ids.Add(desktop.ID)
            Dim fileNames As New List(Of String)
            For Each workspace In desktop.Workspaces
                fileNames.Add(workspace.FilePath)
            Next
            WriteSetting(dir, "Desktop" & desktop.ID & "Maximized", desktop.WindowState = Windows.WindowState.Maximized)
            WriteSetting(dir, "Desktop" & desktop.ID & "Workspaces", Join(fileNames.ToArray, "|"))
            WriteSetting(dir, "Desktop" & desktop.ID & "Rect", (New RectConverter).ConvertToString(New Rect(desktop.Left, desktop.Top, desktop.Width, desktop.Height)))
        Next
        WriteSetting(dir, "Desktops", Join(ids.ToArray, ","))
    End Sub
    Private Sub Window_Loaded(ByVal sender As Object, ByVal e As System.Windows.RoutedEventArgs) Handles Me.Loaded

        CType(Application.Current.FindResource("splashScreen"), SplashWindow).Close()

        If Not Directory.Exists("workspaces") Then Directory.CreateDirectory("workspaces")
        If Not Directory.Exists("settings") Then Directory.CreateDirectory("settings")
        If Not File.Exists(GlobalSettingsFileLocation) Then File.WriteAllText(GlobalSettingsFileLocation, "")
        If Not File.Exists(ApplicationSettingsFileLocation) Then File.WriteAllText(ApplicationSettingsFileLocation, "")
        Dim box = ShowInfoBoxAsync("Loading desktops...")
        Dim desktopIDs As List(Of String) = Split(ReadSetting(ApplicationSettingsFileLocation, "Desktops", ""), ",").ToList
        For Each id In desktopIDs
            If id <> "" Then
                Dim desktop As New DesktopWindow
                Dim rect As Rect = (New RectConverter).ConvertFromString(ReadSetting(ApplicationSettingsFileLocation, "Desktop" & id & "Rect", (New RectConverter).ConvertToString(New Rect(200, 200, 800, 800))))
                desktop.Left = rect.Left
                desktop.Top = rect.Top
                desktop.Width = rect.Width
                desktop.Height = rect.Height
                If ReadSetting(ApplicationSettingsFileLocation, "Desktop" & id & "Maximized", "False") = "True" Then desktop.WindowState = Windows.WindowState.Maximized
                Dim workspaces As List(Of String) = Split(ReadSetting(ApplicationSettingsFileLocation, "Desktop" & id & "Workspaces", ""), "|").ToList
                For Each fileName In workspaces
                    If fileName <> "" Then desktop.LoadWorkspace(fileName)
                Next
                desktop.Show()
            End If
        Next
        If desktopIDs.Count = 0 OrElse (desktopIDs.Count = 1 AndAlso desktopIDs(0) = "") Then
            Dim desktop As New DesktopWindow
            desktop.Show()
        End If
        box.Close()
    End Sub
    Private Sub Window_Closing(ByVal sender As Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles Me.Closing
        My.Application.SaveSettings()
    End Sub
    Private Sub Window_Closed(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Closed
        End
    End Sub

    Public ReadOnly Property Desktops As List(Of DesktopWindow)
        Get
            Dim wins As New List(Of DesktopWindow)
            For Each item In Application.Current.Windows
                If TypeOf item Is DesktopWindow Then
                    wins.Add(item)
                End If
            Next
            Return wins
        End Get
    End Property

End Class
